package com.zxkj.commonpatrol.ui;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.View;

import com.czy.basecommon.utils.base.BaseVmDataBindingActivity;
import com.czy.basecommon.utils.base.StateData;
import com.czy.basecommon.utils.constant.ItrackConstant;
import com.czy.basecommon.utils.tools.ActivityTool;
import com.jeremyliao.liveeventbus.LiveEventBus;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.xuexiang.xui.widget.dialog.MiniLoadingDialog;
import com.zxkj.commonpatrol.BR;
import com.zxkj.commonpatrol.R;
import com.zxkj.commonpatrol.adapt.ProjectDetailAdapt;
import com.zxkj.commonpatrol.adapt.ProjectListAdapt;
import com.zxkj.commonpatrol.databinding.ActivityProjectCheckPointListBinding;
import com.zxkj.commonpatrol.model.entity.SelectProjectEntrity;
import com.zxkj.commonpatrol.model.response.InspectionDetailResponse;
import com.zxkj.commonpatrol.viewmodel.ProjectCheckPointListViewModel;

import java.util.List;

public class ProjectCheckPointListActivity extends BaseVmDataBindingActivity<ActivityProjectCheckPointListBinding, ProjectCheckPointListViewModel> {
    private int colorAccent;
    private int btn_yellow;
    private int green;
    private int white;
    private int mCurrent=0;

    private RecyclerView recyclerView;
    private ProjectDetailAdapt adapt;
    private String proNo;//類別編號   名字反了
    private String routeTypeCode;//路線編號
    private int PageIndex=1;
    private int PageSize=30;
    private String Fiter;
    private MiniLoadingDialog loadingDialog;
    private SmartRefreshLayout smartRefreshLayout;
    private String proName;
    private String Name;
    private boolean isShow=false;


    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_project_check_point_list;
    }

    @Override
    public int initVariableId() {
        return BR.ProjectCheckPointListViewModel;
    }

    @Override
    protected void setListener() {
        super.setListener();
        //pending
        binding.tvUpcoming.setOnClickListener(v->{

            if (mCurrent==0){
                //不處理
            }else{
                binding.clDash.setBackgroundColor(btn_yellow);
                binding.tvUpcoming.setEnabled(false);
                binding.tvUpcoming.setTextColor(btn_yellow);
                binding.tvPending.setEnabled(true);
                binding.tvPending.setTextColor(white);
                binding.tvReject.setEnabled(true);
                binding.tvReject.setTextColor(white);
                //刷新數據
                if (loadingDialog==null){
                    loadingDialog= new MiniLoadingDialog(this, "加載中...");
                }
                loadingDialog.show();
                mCurrent=0;
                viewModel.getCheckList(routeTypeCode,Fiter,mCurrent,PageSize,PageIndex);
            }

        });
        //completeed
        binding.tvPending.setOnClickListener(v->{
            if (mCurrent==1){

            }else{
                binding.clDash.setBackgroundColor(green);
                binding.tvUpcoming.setEnabled(true);
                binding.tvUpcoming.setTextColor(white);
                binding.tvPending.setEnabled(false);
                binding.tvPending.setTextColor(green);
                binding.tvReject.setEnabled(true);
                binding.tvReject.setTextColor(white);
                //刷新數據
                //刷新數據
                if (loadingDialog==null){
                    loadingDialog= new MiniLoadingDialog(this, "加載中...");
                }
                loadingDialog.show();
                mCurrent=1;
                viewModel.getCheckList(routeTypeCode,Fiter,mCurrent,PageSize,PageIndex);
            }

        });
        //all
        binding.tvReject.setOnClickListener(v->{
            if (mCurrent!=-1){
                binding.clDash.setBackgroundColor(colorAccent);
                binding.tvUpcoming.setEnabled(true);
                binding. tvUpcoming.setTextColor(white);
                binding. tvPending.setEnabled(true);
                binding. tvPending.setTextColor(white);
                binding. tvReject.setEnabled(false);
                binding. tvReject.setTextColor(colorAccent);
                //刷新數據
                if (loadingDialog==null){
                    loadingDialog= new MiniLoadingDialog(this, "加載中...");
                }
                loadingDialog.show();
                mCurrent=-1;
                viewModel.getCheckList(routeTypeCode,Fiter,mCurrent,PageSize,PageIndex);
            }

        });
       binding.ivSearch.setOnClickListener(v->{
           Bundle bundle=new Bundle();
           bundle.putInt("IsEdit",1);
           bundle.putString("routeTypeCode",routeTypeCode);
           startActivity(AddCheckPointActivity.class,bundle);
       });
       binding.ivHeadSearch.setOnClickListener(v->{
           Fiter=binding.edSeach.getTextEx();
           binding.multipleProjectListView.showLoading();
           viewModel.getCheckList(routeTypeCode,Fiter,mCurrent,PageSize,PageIndex);
       });

        smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                binding.multipleProjectListView.showLoading();
                viewModel.getCheckList(routeTypeCode,Fiter,mCurrent,PageSize,PageIndex);
            }
        });
        binding.multipleProjectListView.setOnRetryClickListener(v->{
            binding.multipleProjectListView.showLoading();
            viewModel.getCheckList(routeTypeCode,Fiter,mCurrent,PageSize,PageIndex);
        });
        binding.qkBack.setOnClickListener(v->{
            ActivityTool.getInstance().popOneActivity(this);
        });

        binding.edSeach.setOnFocusChangeListener((v,h)->{
            if (h){
                binding.ivHeadSearch.setImageResource(R.drawable.search_biss);
            }else{
                binding.ivHeadSearch.setImageResource(R.drawable.search);
            }
        });
        binding.btnSearch.setOnClickListener(v->{
            if (!isShow){
                binding.rlUpcoming.setVisibility(View.VISIBLE);
                isShow=true;
            }else{
                binding.rlUpcoming.setVisibility(View.GONE);
                isShow=false;
                Fiter="";
                binding.edSeach.setTextEx("");
            }
        });
    }


    @Override
    protected void initUI() {

         Name = getIntent().getStringExtra("Name");
         proName = getIntent().getStringExtra("proName");

        binding.tvTitle.setText(Name);
        routeTypeCode = getIntent().getStringExtra("ProNo");
        proNo = getIntent().getStringExtra("RouteTypeCode");
        white= getResources().getColor(R.color.white);
        colorAccent = getResources().getColor(R.color.colorAccent);
        btn_yellow = getResources().getColor(R.color.btn_yellow);
        green = getResources().getColor(R.color.green);
        LinearLayoutManager layoutManager =new LinearLayoutManager(this);
        recyclerView = (RecyclerView) findViewById(R.id.recy_project_check_pointlist_project);
        recyclerView.setLayoutManager(layoutManager);
         smartRefreshLayout = (SmartRefreshLayout) findViewById(R.id.refreshLayout_recy_project_check_point_list);
    }

    @Override
    protected void initData() {
        if (loadingDialog==null){
            loadingDialog= new MiniLoadingDialog(this, "加載中...");
        }
        loadingDialog.show();
        viewModel.getCheckList(routeTypeCode,Fiter,mCurrent,PageSize,PageIndex);
    }
    @Override
    protected void dataObserver() {
        super.dataObserver();
        viewModel.CheckPonints.observe(this, new Observer<StateData<List<InspectionDetailResponse.InspectPoint>>>() {
            @Override
            public void onChanged(StateData<List<InspectionDetailResponse.InspectPoint>> listStateData) {
                smartRefreshLayout.finishRefresh();
                if (loadingDialog!=null){
                    loadingDialog.dismiss();
                }
                if (listStateData.getStatus()==StateData.DataStatus.SUCCESS){
                    if (listStateData.getData().size()>0){
                        binding.multipleProjectListView.showContent();
                        if (adapt==null){
                            adapt =new ProjectDetailAdapt(R.layout.item_projects_check_point_list,listStateData.getData(),routeTypeCode);
                            recyclerView.setAdapter(adapt);

                            adapt.setmDataCallBack(new ProjectDetailAdapt.MyProjectDetailCallBack() {
                                @Override
                                public void onDataSuccessCallBack(Object o) {
                                    int i=(Integer)o;

                                    Bundle bundle=new Bundle();
                                    bundle.putInt("IsEdit",0);
                                    bundle.putString("proName",proName);
                                    bundle.putString("name",Name);
                                    bundle.putString("routeCode",routeTypeCode);
                                    bundle.putString("routeTypeCode",adapt.getData().get(i).getCode());
                                    startActivity(AddCheckPointActivity.class,bundle);
                                }

                                @Override
                                public void onDataFailCallBack(Object o, Object t1) {

                                }
                            });
                        }else{
                            adapt.setNewData(listStateData.getData());
                            recyclerView.setAdapter(adapt);
                        }
                    }else{
                        binding.multipleProjectListView.showEmpty();
                    }

                }else{
                    binding.multipleProjectListView.showError();
                }



            }
        });
        LiveEventBus.get("refresh",String.class).observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                if (loadingDialog==null){
                    loadingDialog= new MiniLoadingDialog(ProjectCheckPointListActivity.this, "加載中...");
                }
                loadingDialog.show();
                viewModel.getCheckList(routeTypeCode,Fiter,mCurrent,PageSize,PageIndex);
            }
        });

    }
}
